//
//  YGMainTabBarViewController.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/10.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

class YGMainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 定义闭包
        let composeCallBack = { () -> () in
            
//            print("Modal出控制器")
//            YGPrint(items: "Modal出控制器")
            if !YGUserAccountViewModel.sharedInstance.isUserLogin {
                // modal出授权控制器
                let oauthVC = YGOauthViewController()
                let nav = UINavigationController(rootViewController: oauthVC)
                self.presentViewController(nav, animated: true, completion: nil)
            } else {
                // modal出  创建微博控制器
                let composeVC = YGComposeViewController()
                let nav = UINavigationController(rootViewController: composeVC)
                self.presentViewController(nav, animated: true, completion: nil)
            }
        }
        
        // 创建自定义tabBar
        let mainTabBar = YGMainTabBar()
        
        // 传递闭包
        mainTabBar.composeCallBack = composeCallBack
        
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()

//        tabBar.tintColor = UIColor.orangeColor()
    }
    
    ///  添加子控制器
    func addChildViewControllers() {
        
        // 首页
        let homeVC = YGHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        // 信息
        let MessVC = YGMessViewController()
        addChildViewController(MessVC, title: "信息", imageName: "tabbar_message_center")
        // 发现
        let discVC = YGDiscViewController()
        addChildViewController(discVC, title: "发现", imageName: "tabbar_discover")
        // 我
        let profVC = YGProfViewController()
        addChildViewController(profVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    ///  根据数据创建子控制器
    func addChildViewController(controller: UIViewController,title: String, imageName: String) {
        
        controller.title = title
        // 设置图片 tabbar_profile_selected
        controller.tabBarItem.image = UIImage(named: imageName)
        // 创建选中时图片
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 设置字体
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        // 添加到tabBar控制器 // 包装导航控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    

}





















